package run;

import com.SpringApplicationApps;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import utils.Utils;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.HashMap;

@SpringBootTest(classes = {ActivitiSpringBootDemo.Dummy.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class ActivitiSpringBootDemo {
    @Autowired
    GenericApplicationContext context;


    @Import(value = {ActivitiSpringBootDemo.class, SpringApplicationApps.class})
    @Configuration
    public class Dummy {
    }

    Object lock = new Object();

    @Test
    public void verify() throws InterruptedException {
        Assertions.assertNotNull(context.getBean(DataSource.class));
        Utils.print(context);

        synchronized (lock) {
            lock.wait();
        }
    }

    @Test
    public void load() throws InterruptedException {
        ProcessEngine processEngine = context.getBean(ProcessEngine.class);
        HashMap<String, Object> variables = new HashMap<>() {{
            put("employeeName", "john");
            put("numberOfDays", 3L);
            put("reason", "i need a break");
            put("startDate","11-01-2022 00:00");
        }};
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance vacationRequest = runtimeService.startProcessInstanceById("demo-old:2:368218f6-485c-11ed-975c-00ff07a359cf", variables);
        long count = runtimeService.createProcessInstanceQuery().count();
        Assertions.assertEquals(1L, 1);
        synchronized (lock) {
            lock.wait();
        }
    }

}
